<?php
class Managearticles_Data_Addform {
	
	private $form;
	private $database;
	
	public function __construct(){
		$this->form = new Core_Driver_Firstform();	
		$this->database = new Core_Driver_MysqlImproved();
	}
	
	public function getForm(){
		$categories = $this->getCategory();
		$access_levels = $this->getAccessLevel();
		$article['content'] .= $this->form->startForm(
				array(
						"id" => "pageEditForm",
						"name" => "pageEditForm",
						"method" => "post",
						"action" => SERVER_ROOT . "?managearticles&page=added"
				)
		);
		$article['content'] .= $this->form->inputForm(
			array(
			"type" => "hidden",
					"value" => "addForm",
					"name" => "addForm"
		),
				null
		);
		$article['content'] .= ADD_ARTICLES_MESSAGE;
		$article['content'] .= "<ul>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "page_name"
				),
				ADD_ARTICLES_PAGE_NAME,
				null
		);
		$article['content'] .= "<br>";
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "pageName",
						"name" => "pageName",
						"type" => "text",
						"placeholder" => ADD_ARTICLES_PAGE_NAME_PLACEHOLDER,
						"maxlength" => "255"
				),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "page_title"
				),
				ADD_ARTICLES_PAGE_TITLE,
				null
		);
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "pageTitle",
						"name" => "pageTitle",
						"placeholder" => ADD_ARTICLES_PAGE_TITLE_PLACEHOLDER,
						"type" => "text",
						"maxlegth" => "255"
				),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "</li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageMetatitle"
				),
				ADD_ARTICLES_PAGE_META_TITLE,
				null
		);
		$article['content'] .= "<br>";
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "pageMetaTitle",
						"name" => "pageMetaTitle",
						"type" => "text",
						"placeholder" => ADD_ARTICLES_PAGE_META_DESCRIPTION_PLACEHOLDER,
						"maxlength" => "255"
				),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
			array(
			"for" => "avalaible"
		), 
				ADD_ARTICLES_PAGE_ACTIVE,
				null
		);
		$article['content'] .= $this->form->selectForm(
			array(
			"id" => "available",
					"name" => "available"
		),
				null,
				array(
			"1" => "true",
						"0" => "false"
		)
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
			array(
			"for" => "accessLevel"
		),
				ADD_ARTICLES_PAGE_ACCESS_LEVEL,
				null
		);
		$article['content'] .= $this->form->selectForm(
			array(
			"id" => "accessLevel",
					"name" => "accessLevel"
		),
				null,
				$access_levels
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "category"
				),
				ADD_ARTICLES_PAGE_CATEGORY,
				null
		);
		$article['content'] .= $this->form->selectForm(
				array(
						"id" => "category",
						"name" => "category"
				),
				"required",
				$categories
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageKeywords"
				),
				ADD_ARTICLES_PAGE_KEYWORDS,
				null
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageKeywords",
						"maxlegth" => "1000",
						"id" => "pageKeywords",
						"placeholder" => ADD_ARTICLES_PAGE_KEYWORDS_PLACEHOLDER,
						"syle" => "heght: 5 em;"
				),
				"required",
				null
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageMetaDescription"
				),
				ADD_ARTICLES_PAGE_META_DESCRIPTION,
				null
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageMetaDescription",
						"maxlegth" => "1000",
						"id" => "pageMetadescription",
						"placeholder" => ADD_ARTICLES_PAGE_META_DESCRIPTION_PLACEHOLDER,
						"style" => "height: 5 em;"
				),
				"required",
				null
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageDescription"
				),
				ADD_ARTICLES_PAGE_DESCRIPTION,
				null
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageDescription",
						"maxlenth" => "1000",
						"id" => "pageDescription",
						"placeholder" => ADD_ARTICLES_PAGE_DESCRIPTION_PLACEHOLDER,
						"style" => "height: 5 em;"
				),
				"required",
				null
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageContent"
				),
				ADD_ARTICLES_PAGE_CONTENT,
				null
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageContent",
						"maxlenght" => "100000",
						"id" => "pageContent",
						"placeholder" => ADD_ARTICLES_PAGE_CONTENT_PLACEHOLDER,
						"style" => "height: 5em;"
				),
				"required",
				null
		);
		$article['content'] .= "</li>";
		$article['content'] .= "</ul>";
		$article['content'] .= '<div class="buttons">';
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "submitForm",
						"name" => "submit",
						"type" => "submit",
						"value" => ADD_ARTICLES_BUTTON
				),
				null
		);
		$article['content'] .= "</div>";
		$article['content'] .= $this->form->endForm();
		return $article;
	}
	
	private function getAccessLevel(){
		$this->database->connect();
		$this->database->prepare(
			"
				select * from privileges order by privileges_types asc;
				"
		);
		$this->database->query();
		$access_levels = array();
		while($result = $this->database->fetch('array')){
			$access_levels[$result['privileges_types']] = "(".$result['privileges_types'].") ".$result['privileges_name'];
		}
		return $access_levels;
	}
	
	private function getCategory(){
		$this->database->connect();
		$this->database->prepare(
			"
				select * from category_settings;
				"
		);
		$this->database->query();
		$categories = array();
		while($categoryData = $this->database->fetch('array')){
			$categories[$categoryData['category_name']] = $categoryData['category_name'];
		}
		return $categories;
	}
}